/*
 * Copyright (C) 2012, EADS France
 * 
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2.1 of the License, or (at your option) any later version.
 * 
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.
 */
package xowl.gmi.view;

import xowl.gmi.environment.Locale;

/**
 *
 * @author Laurent WOUTERS
 */
public class WinProjects extends javax.swing.JPanel implements ProjectTreeUpdater {
    public static interface Actions {
        void onFileNewProject();
        void onFileNewDiagram();
        void onFileOpenProject();
    }
    
    private Actions actions;
    private ViewStyle style;
    private WinProjectsNode projectsRoot;
    private javax.swing.tree.DefaultTreeModel projectsModel;
    private javax.swing.JTree projectsTree;
    private javax.swing.JPopupMenu projectsMenu;
    
    public WinProjects(Actions actions, Locale locale, ViewStyle style) {
        this.actions = actions;
        this.style = style;
        projectsRoot = new WinProjectsNode("<root>") {
            @Override public java.awt.Component getRendering(ViewStyle style, boolean selected) { return new javax.swing.JLabel("<root>"); }
        };
        projectsModel = new javax.swing.tree.DefaultTreeModel(projectsRoot);
        projectsMenu = new javax.swing.JPopupMenu();
        javax.swing.JMenuItem itemNewProject = new javax.swing.JMenuItem(locale.fileNewProject_text, IconProvider.getEmbedded("project_add.png"));
        itemNewProject.addMouseListener(new java.awt.event.MouseAdapter() {
            @Override public void mouseReleased(java.awt.event.MouseEvent evt) { WinProjects.this.actions.onFileNewProject(); }
        });
        javax.swing.JMenuItem itemOpenProject = new javax.swing.JMenuItem(locale.fileOpenProject_text, IconProvider.getEmbedded("project_open.png"));
        itemOpenProject.addMouseListener(new java.awt.event.MouseAdapter() {
            @Override public void mouseReleased(java.awt.event.MouseEvent evt) { WinProjects.this.actions.onFileOpenProject(); }
        });
        javax.swing.JMenuItem itemNewDiagram = new javax.swing.JMenuItem(locale.fileNewDiagram_text, IconProvider.getEmbedded("diagram_add.png"));
        itemNewDiagram.addMouseListener(new java.awt.event.MouseAdapter() {
            @Override public void mouseReleased(java.awt.event.MouseEvent evt) { WinProjects.this.actions.onFileNewDiagram(); }
        });
        projectsMenu.add(itemNewProject);
        projectsMenu.add(itemOpenProject);
        projectsMenu.add(itemNewDiagram);
        projectsTree = new javax.swing.JTree(projectsModel);
        projectsTree.setRootVisible(false);
        projectsTree.setShowsRootHandles(true);
        projectsTree.setCellRenderer(new javax.swing.tree.TreeCellRenderer() {
            @Override public java.awt.Component getTreeCellRendererComponent(javax.swing.JTree tree, Object value, boolean selected, boolean expanded, boolean leaf, int row, boolean hasFocus) {
                if (value instanceof WinProjectsNode)
                    return ((WinProjectsNode)value).getRendering(WinProjects.this.style, selected);
                else
                    return getDefaultCell(value);
            }
        });
        projectsTree.addMouseListener(new java.awt.event.MouseAdapter() {
            @Override public void mouseReleased(java.awt.event.MouseEvent evt) {
                if (evt.getButton() == java.awt.event.MouseEvent.BUTTON1)
                    return;
                javax.swing.tree.TreePath path = projectsTree.getPathForLocation(evt.getX(), evt.getY());
                if (path == null) {
                    projectsMenu.show(projectsTree, evt.getX(), evt.getY());
                } else {
                    projectsTree.setSelectionPath(path);
                    Object node = path.getLastPathComponent();
                    if (node instanceof ProjectNode)
                        ((ProjectNode)node).getMenu().show(projectsTree, evt.getX(), evt.getY());
                    else if (node instanceof DiagramNode)
                        ((DiagramNode)node).getMenu().show(projectsTree, evt.getX(), evt.getY());
                }
            }
        });
        this.setLayout(new java.awt.BorderLayout());
        this.add(projectsTree, java.awt.BorderLayout.CENTER);
    }
    
    public void addProjectNode(ProjectNode pv) {
        pv.setUpdater(this);
        insertNode(projectsRoot, pv);
    }
    public void removeProjectNode(ProjectNode pv) {
        pv.setUpdater(null);
        removeNode(pv);
    }
    @Override public void insertNode(WinProjectsNode parent, WinProjectsNode node) {
        parent.insert(node, parent.getChildCount());
        node.setWindow(this);
        projectsModel.reload(parent);
        projectsTree.makeVisible(new javax.swing.tree.TreePath(node));
    }
    @Override public void removeNode(WinProjectsNode node) {
        ((javax.swing.tree.MutableTreeNode)node.getParent()).remove(node);
        projectsModel.reload();
    }
    
    private java.awt.Component getDefaultCell(Object value) {
        return new javax.swing.JLabel(value.toString());
    }
    
    public ProjectNode getSelectedProject() {
        javax.swing.tree.TreePath path = projectsTree.getSelectionPath();
        if (path == null)
            return null;
        for (Object element : path.getPath()) {
            if (element instanceof ProjectNode)
                return (ProjectNode)element;
        }
        return null;
    }
}
